package billingStatistics;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author Xu,
 * @data 2020/12/13，
 * @time 22:48
 * 计费规则如下：
 * 1、进场后在30分钟内出场的车辆，免收费；
 * 2、停放在2小时内（含2小时），收费10元；从第三小时起，每小时2元。
 * 3、不封顶。
 * 请按照月份，计算自己学号停车场收入。（注：按月统计车辆出场时的收入，即车辆4月进场，5月出场，数据放在5月）
 *
 */
public class BillingStatistics {
    private static ArrayList<ArrayList<String>> log = new ArrayList<>();
    private static ArrayList<ArrayList<String>> myLog = new ArrayList<>();
    private static long time;
    private static long jan_income = 0, feb_income = 0, mar_income = 0, apr_income = 0, may_income = 0, jun_income = 0;
    public static void main(String[] args) throws ParseException {
        long start = System.currentTimeMillis();
        String path = "./src/carsLog.txt";
        readData(path);
        searchData();
        parkTime();
        long end = System.currentTimeMillis();
        System.out.println("runtime: " + (end - start)/1000.0 + " s");
        showResult();
    }

    /**
     * 统计每个月收益
     */
    public static void parkTime() throws ParseException {
        String str;
        for (int i = 0; i < myLog.size(); i++) {
            if ("in".equals(myLog.get(i).get(3))) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date1 = sdf.parse(myLog.get(i).get(0));
                for (int j = i; j < myLog.size(); j++) {
                    if (myLog.get(j).get(2).equals(myLog.get(i).get(2)) && "out".equals(myLog.get(j).get(3))) {
                        str = myLog.get(j).get(0).substring(5,7);
                        Date date2 = sdf.parse(myLog.get(j).get(0));
                        time = (date2.getTime() - date1.getTime()) / 1000;
                        if(time > 30*60 && time <=2*60*60){
                            findMonth(str,10);
                        }else if(time > 2*60*60){
                            if(time%(60*60) != 0){
                                findMonth(str,10+((long)((time-2*60*60)/(60*60))+1)*2);
                            }else{
                                findMonth(str,10+((long)((time-2*60*60)/(60*60)))*2);
                            }
                        }
                        break;
                    }
                }
            }
        }
    }

    /**
     * 将对应月份的收入累计
     * @param str
     * @param pay
     */
    public static void findMonth(String str , long pay){
        switch (str){
            case "01": jan_income += pay; break;
            case "02": feb_income += pay; break;
            case "03": mar_income += pay; break;
            case "04": apr_income += pay; break;
            case "05": may_income += pay; break;
            case "06": jun_income += pay; break;
            default:break;
        }

    }


    /**
     * 找到自己学号的停车场
     */
    public static void searchData() {
        for (int i = 0; i < log.size(); i++) {
            if ("201825010318".equals(log.get(i).get(1))) {
                myLog.add(log.get(i));
            }
        }

    }

    /**
     * 读取数据
     *
     * @param path
     * @return
     */
    public static void readData(String path) {
        ArrayList<String> d = null;
        try {
            InputStreamReader isr = new InputStreamReader(new FileInputStream(new File(path)));
            BufferedReader br = new BufferedReader(isr);
            String str = null;
            while ((str = br.readLine()) != null) {
                d = new ArrayList<String>();
                String[] str1 = str.split(",");
                for (int i = 0; i < str1.length; i++) {
                    d.add(str1[i]);
                }
                log.add(d);
            }
            br.close();
            isr.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("读取文件内容出错！");
        }
    }

    /**
     * 输出结果
     */
    public static void showResult(){
        System.out.println("按月统计收入：");
        System.out.println("2020-01: " + jan_income + "元");
        System.out.println("2020-02: " + feb_income + "元");
        System.out.println("2020-03: " + mar_income + "元");
        System.out.println("2020-04: " + apr_income + "元");
        System.out.println("2020-05: " + may_income + "元");
        System.out.println("2020-06: " + jun_income + "元");
    }

    public static void show(){
        for(int i=0;i<myLog.size();i++){
            System.out.println(myLog.get(i));
        }
    }

}
